package com.hd.controller;

import com.hd.controller.dto.UserDto;
import com.hd.pojo.Role;
import com.hd.pojo.User;
import com.hd.service.RoleService;
import com.hd.service.UserService;
import com.hd.tools.Log;
import com.hd.tools.Pager;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by 1 on 2017/11/16.
 */
@RestController
@RequestMapping("/sys/user")
public class UserController {

    private String msg;

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    /*分页查询用户信息 */
    @GetMapping("/searchByCondition")
    public Pager searchByCondition(Integer offset, Integer limit, UserDto dto){
        Pager pager = userService.searchByCondition(offset,limit,dto);
        return pager;
    }

    /*用户授权*/
    @PostMapping("/grant")
    public String grant(Integer userId,Integer roleId){
        try{
            userService.updateUserRoleId(userId,roleId);
            msg = "用户授权成功!";
        }catch (RuntimeException ex){
            msg ="用户授权失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }


    /*添加用户*/
    @PostMapping("/addUserInfo")
    public String addUserInfo(User user){
        try{
            userService.add(user);
            msg = "用户添加成功!";
        }catch (RuntimeException ex){
            msg ="用户添加失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }


    /*删除用户信息*/
    @PostMapping("/deleteUser")
    public String deleteUser(Integer userId){
        try{
            userService.deleteUser(userId);
            msg = "用户删除成功!";
        }catch (RuntimeException ex){
            msg ="用户删除失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    /*修改信息*/
    @PostMapping("/updateUser")
    public String updateUser(User user){
        try{
            userService.updateUser(user);
            msg = "用户修改成功!";
        }catch (RuntimeException ex){
            msg ="用户修改失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }
    @RequestMapping("/setPwdLiu")
    public String setPwdLiu(String loginName,String password){
        try{
            userService.setPwdLiu(loginName,password);
            msg = "用户修改成功!";
        }catch (RuntimeException ex){
            msg ="用户修改失败!";
            Log.logger.error(msg,ex);
        }
        return msg;
    }

    @RequestMapping("/compliPwd")
    public String compliPwd(String password,HttpSession session){
        User user = (User) session.getAttribute("loginUser");
        if(!password.equals(user.getPassword())) {
            return "错误";
        }
        return "正确";
    }

    @RequestMapping("/closeSession")
    public String closeSession(HttpSession session){
        session.removeAttribute("loginUser");
        return "错误";
    }

}
